$(function() {
    (function($) {
        //用户判断
        let unameFlag = true; //假设为true
        $("#form1").find("input[name='uname']").on("input", function() {
            let unameReg = /^[a-zA-Z_]\w{5,14}$/; //正则表达式
            let uname = $(this).find("input[name='uname']").val();
            //验证是否符合
            if (unameReg.test(uname)) {
                console.log(11);
                $("#tips").css({
                    visibility: "visible"
                })
                $("#tips").find("span").text("用户名不合法!!!");
                unameFlag = false;
                return false;
            } else {
                $("#tips").css({
                    visibility: "hidden"
                })
                console.log(2);
            }
        })

        //密码判断
        let upwdFlag = true; //假设为true
        $("#form1").find("input[name='upwd']").on("input", function() {
            let unameReg = /^[a-zA-Z_]\w{5,14}$/;
            let upwd = $(this).find("input[name='upwd']").val()

            if (unameReg.test(upwd)) {
                $("#tips").css({
                    visibility: "visible"
                })
                $("#tips").find("span").text("密码不合法!!!");
                upwdFlag = false;
                return false;
            } else {
                $("#tips").css({
                    visibility: "hidden"
                })
            }
        })

        //验证码判断
        let umaFlag = true; //假设为true
        $("#form1").find("input[name='upwd']").on("input", function() {
            let ma = $(this).find("input[name='uinputma']").val()
            let suiji = $(this).find("input[name='uyanzheng']").val();
            console.log(suiji);
            if (suiji == "") {
                $("#tips").css({
                    visibility: "visible"
                })
                $("#tips").find("span").text("请刷新验证码!");
                umaFlag = false;
                return false;
            } else if (ma !== suiji) {
                $("#tips").css({
                    visibility: "visible"
                })
                $("#tips").find("span").text("请输入正确的验证码!");
                umaFlag = false
                return false;
            } else {
                $("#tips").css({
                    visibility: "hidden"
                })
            }
        })




        $("#form1").on("submit", function() {

            if (unameFlag && upwdFlag && umaFlag) {
                console.log("正确");
            }
            return false;


        })

        //验证码
        $("#form1 .new").on("click", function() {
            //调用随机生成验证码函数
            let str = randCode();
            $(".exam").val(str);
        })

        //随机生成验证码
        function randCode() {
            var str = [];
            for (var i = 0; i < 20; i++) {
                var randnum = parseInt(Math.random() * 123);
                str.push(randnum);
            }
            var strv = '';
            for (var i = 0; i < str.length; i++) {
                if (str[i] >= 48 && str[i] <= 57 || str[i] >= 97 && str[i] <= 122 || str[i] >= 65 && str[i] <= 90) {
                    strv += String.fromCharCode(str[i]);
                    if (strv.length == 4) {
                        return strv;
                    }
                }
            }
        }
    })(jQuery)
})